<?php

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018/12/29
 * Time: 08:27
 */

namespace app\index\model;


use think\Model;

class Dynamic extends Model
{


    /**
     * 获取用户信息
     * @param $value
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserModelAttr($value)
    {
        return db('user')->field('id,nickname,headurl')->where('id', $value)->find();
    }

    public function getImagesAttr($value)
    {
        return json_decode($value);
    }

    // 获取评论数
    public function getCommentCountAttr($value)
    {
        return db('comment')->where('pid', $value)->count();
    }

    // 获取点赞数
    public function getLikesCountAttr($value)
    {
        return db('collection')->where('pid', $value)->where('type', 'like')->count();
    }

    // 获取分享数
    public function getShareCountAttr($value)
    {
        return db('collection')->where('pid', $value)->where('type', 'share')->count();
    }

    public function getTimeAttr($value)
    {
        $minute = round((time() - strtotime($value)) / 60);
        if ($minute > 0 && $minute < 60) {
            return $minute . '分钟前';
        } else if ($minute >= 60 && $minute < ( 60 * 24)) {
            return round($minute / 60) . '小时前';
        } else {
            return date('Y-m-d', strtotime($value));
        }
    }
}